import EventCenter from "../../lib/EventCenter";
import MyDialogManager from "../../lib/MyDialogManager";
import Sdk from "../../lib/Sdk";
import SoundManager from "../../lib/SoundManger";
import { TipManager } from "../../lib/TipManager";
import {ui} from "../../ui/layaMaxUI";
import EventEunm from "../data/EventEunm";
import PlayerData from "../data/PlayerData";
import GridManager from "../GridManager";
import RankManager from "../RankManager";
export default class FailDialog extends ui.FailDialogUI{
    onOpened(_revive:boolean){
        this.closeBtn.on(Laya.Event.CLICK,this,this.closeClick);
        this.homeBtn.on(Laya.Event.CLICK,this,this.closeClick)
        this.restBtn.on(Laya.Event.CLICK,this,this.restClick);
        this.reviveBtn.on(Laya.Event.CLICK,this,this.reviveClick);
        this.reviveBtn.visible=_revive;
        Sdk.getIns().showBanner();
        SoundManager.playSound("game_over.mp3");
        PlayerData.getIns().addLive(-1);
        if(GridManager.getIns().isWuJiMode){
            if(PlayerData.getIns().setHisScore(GridManager.getIns().score)){
                RankManager.getIns().updateScore(GridManager.getIns().score);
            }
        }
        Sdk.getIns().showInterstitialAd();
        this.reviveBtn.getChildByName("tip")["text"]="分享游戏";
    }

    private closeClick(){
        MyDialogManager.getIns().sceneOpen("StartScene.scene",true);
        this.close();
    }
    private restClick(){
        EventCenter.getIns().event(EventEunm.restgame);
        this.close();
    }

    private reviveClick(){
        Sdk.getIns().share();
        Laya.timer.once(1000,this,()=>{
            EventCenter.getIns().event(EventEunm.revive);
            this.close();
        })
        // Sdk.getIns().showAd((code:number)=>{
        //     if(code==0){
        //         EventCenter.getIns().event(EventEunm.revive);
        //         this.close();
        //     }else{
        //         TipManager.ins.showMsgAlert("观看完整视频才能获取奖励",this);
        //     }
        // });
    }

    onDisable(){
        this.closeBtn.off(Laya.Event.CLICK,this,this.closeClick);
        this.homeBtn.off(Laya.Event.CLICK,this,this.closeClick)
        this.restBtn.off(Laya.Event.CLICK,this,this.restClick);
        this.reviveBtn.off(Laya.Event.CLICK,this,this.reviveClick);
        Sdk.getIns().hideBanner();
    }
}